package hp.com.dao.impl;

import hp.com.dao.BaseDao;
import hp.com.dao.CartDataDAO;
import hp.com.entity.CartData;
import hp.com.entity.Users;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartDataDAOImpl extends BaseDao implements CartDataDAO {
    //通过ID查找cartData数据
    @Override
    public CartData GetCartDataById(int id) throws Exception {
        String sql = "select * from cartdata where dataid=?";
        String sqlName = "select pname,easyName from product where id=?";
        ResultSet rs = executeQuery(sql, new Object[]{id});
        CartData c=null;
        while (rs.next()){
            int dataId = rs.getInt("dataId");
            String userId = rs.getString("userId");
            String itemId = rs.getString("itemId");
            int itemNumber = rs.getInt("itemNumber");
            String SUK = rs.getString("suk");
            String pic = rs.getString("pic");
            ResultSet rsName = executeQuery(sqlName, new Object[]{itemId});
            double price = Double.valueOf(String.format("%.2f",rs.getDouble("price")));
            rsName.next();
            String easyName = rsName.getString("easyName");
            String fullName = rsName.getString("pname");
            double singlePrice=price/itemNumber;
            c = new CartData(dataId,userId,itemId,itemNumber,SUK,pic,price,easyName,fullName,singlePrice);
        }
        return c;
    }

    // 查看购物车
    @Override
    public List<CartData> BuyCarShow(String name) throws Exception {
        ArrayList<CartData> list = new ArrayList<>();
        String sql = "select * from cartdata where userId = ? order by dataId desc";
        String sqlName = "select pname,easyName from product where id=?";
        ResultSet rs = executeQuery(sql, new Object[]{name});
        while (rs.next()){
            int dataId = rs.getInt("dataId");
            String userId = rs.getString("userId");
            String itemId = rs.getString("itemId");
            int itemNumber = rs.getInt("itemNumber");
            String SUK = rs.getString("suk");
            String pic = rs.getString("pic");
            ResultSet rsName = executeQuery(sqlName, new Object[]{itemId});
            double price = Double.valueOf(String.format("%.2f",rs.getDouble("price")));
            rsName.next();
            String easyName = rsName.getString("easyName");
            String fullName = rsName.getString("pname");
            Double singlePrice=(double) Math.round(price/itemNumber * 100) / 100;
            CartData c = new CartData(dataId,userId,itemId,itemNumber,SUK,pic,price,easyName,fullName,singlePrice);
            list.add(c);
        }
        return list;
    }

    //通过ID删除cartData数据
    @Override
    public void DeleteCartData(int id) throws Exception {
        String sql ="delete from cartdata where dataId = ?";
        this.executeUpdate(sql,new Object[]{id});
    }

    @Override
    public void UpdateAmountAndPrice(int dataId, int amount, double newPrice) throws Exception {
        String sql = "update cartdata set price=?, itemNumber=? where dataId=?";
        this.executeUpdate(sql,new Object[]{newPrice,amount,dataId});
    }

    @Override
    public boolean AddDataToCart(CartData cart) throws Exception{
        String sql = "insert into cartdata (userId,itemId,itemNumber,suk,pic,price) values(?,?,?,?,?,?)";
        Object[] obj ={cart.getUserId(),cart.getItemId(),cart.getItemNumber(),cart.getSuk(),cart.getPic(),cart.getPrice()};
       int r =this.executeUpdate(sql,obj);
       if(r==1){
           return true;
       }
       return false;
    }

    @Override
    public Map<String,String> GetTextSUKNPriceSUK(String suk) throws Exception {
        String sql ="select suk_detailtext,suk_price from suk_table where good_suk=?";
        ResultSet rs = this.executeQuery(sql,new Object[]{suk});
        Map<String,String> map = new HashMap<>();
        if (rs.next()){
            map.put("suk",rs.getString("suk_detailtext"));
            map.put("price",rs.getString("suk_price"));
            return map;
        }
        return null;
    }

    @Override
    public String GetFirstPicById(int id) throws Exception {
        String sql="select picname from product where id=?";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
            rs.next();
            String[] list = rs.getString("picname").split(",");
            return list[0];
        }
}
